package com.lizk.study.netty.day004;

import com.lizk.study.netty.entity.ServerContent;
import com.lizk.study.netty.utils.NettyUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

import java.util.Date;

/**
 * @author lizhikui
 * @date 2019/6/18 23:56
 */
public class WebSocketServer {
    public static void main(String[] args) {
        ServerContent serverContent = NettyUtils.createServerContent(NioServerSocketChannel.class);
        serverContent.getServerBootstrap()
                .handler(new LoggingHandler(LogLevel.INFO))
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline()
                        .addLast(new HttpServerCodec())
                        .addLast(new ChunkedWriteHandler())
                        .addLast(new HttpObjectAggregator(8192))
                        .addLast(new WebSocketServerProtocolHandler("/ws"))
                        .addLast(new SimpleChannelInboundHandler<TextWebSocketFrame>() {
                            @Override
                            protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {

                                System.out.println("客户端数据:" + msg.text());
                                ctx.writeAndFlush(new TextWebSocketFrame(new Date().toString()));
                            }

                            @Override
                            public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
                                ctx.writeAndFlush(new TextWebSocketFrame("加入链接"));
                                System.out.println("加入链接");
                            }

                            @Override
                            public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
                                ctx.writeAndFlush(new TextWebSocketFrame("退出链接"));
                                System.out.println("退出链接");
                            }

                            @Override
                            public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
                                ctx.close();
                            }
                        });
                    }
                });
        try {
            NettyUtils.bind(serverContent,9999);
        } catch (InterruptedException e) {
            e.printStackTrace();
            NettyUtils.stop(serverContent);
        }
    }
}
